package com.rankeiot.core.job;



import com.rankeiot.core.MenuManager;
import com.rankeiot.core.anno.Job;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class JobManager {
    static Map<String, JobMeta> jobMap=new ConcurrentHashMap<>();
    static JobHandler defaultTaskHandler=new JobHandler() {};
    private static Boolean isEnabled=null;
    public static void setHandler(JobHandler jh){
        defaultTaskHandler=jh;
    }
    public static Collection<JobMeta> getJobs(){
       return jobMap.values();
    }
    public static JobMeta getJob(String name){
        return jobMap.get(name);
    }

    private static TaskScheduler  taskScheduler;
    private static ScheduledTaskRegistrar registrar;
    @Autowired(required = false)
    public void setTaskScheduler(TaskScheduler _taskScheduler){
        taskScheduler=_taskScheduler;
    }
    @Autowired(required = false)
    public void setRegistrar(ScheduledTaskRegistrar _registrar){
        registrar=_registrar;
    }

//    /**
//     * 是否可用
//     * @return
//     */
//    public static boolean isJobEnable(){
//        if(isEnabled==null){
//            try {
//                Class.forName("cn.hutool.cron.Scheduler");
//                isEnabled=true;
//            } catch (ClassNotFoundException e) {
//                isEnabled=false;
//            }
//        }
//        return isEnabled;
//    }


    public static void initJobs(ApplicationStartedEvent event){
        ApplicationContext ctx=event.getApplicationContext();
      //  MenuManager.addExcludeMenu();
      Map<String,Object> jobs =  ctx.getBeansWithAnnotation(Job.class);
      if(jobs.size()==0){
          return;
      }
      for(Map.Entry<String,Object> entry:jobs.entrySet()){
          System.out.println(entry.getKey()+":"+entry.getValue());
          Object v=entry.getValue();
          String name=entry.getKey();
          if(v instanceof Runnable){
              Job anno=v.getClass().getAnnotation(Job.class);
              JobMeta meta=new JobMeta(name,anno.cron(),(Runnable) v);
              meta.setTitle(anno.title());
              meta.setDesc(anno.desc());
              jobMap.put(name,meta);

          }else{
              log.warn("任务 {} 不是Runnable实现！",name);
          }
      }
    }
    public static void start(){
        for(JobMeta jm:getJobs()){
            jm.schedule(taskScheduler);
        }
    }
    public static void schedule(String name){
       JobMeta jm= getJob(name);
       if(jm!=null){
           jm.schedule(taskScheduler);
       }
    }
    public static void cancelSchedule(String name){
        JobMeta jm= getJob(name);
        if(jm!=null){
            jm.cancelSchedule();
        }
    }

}
